import { useState, useEffect } from 'react'
import { getPageList } from '@/api/article'

function useAuth() {
    const isLogin = sessionStorage.getItem('token')? true: false
    return {
        isLogin
    }
}

function useAddKey(list: Array<any>) {
    list.forEach(item => {
        item.key = item._id
    })
    return list
}


function usePageInfo(url: string) {
    const [pageInfo, setPageInfo] = useState({
        pagenum: 1,
        pagesize: 5
    })
    const [list, setList] = useState([])
    const [total, setTotal] = useState(0)

    const changePage = (page: any) => {
        setPageInfo({
            pagenum: page.current,
            pagesize: page.pageSize
        })
        // console.log('抽离了', page)
    }

    useEffect(() => {
        getPageList(url, pageInfo).then(res => {
            setList(res.data.data)
            setTotal(res.data.total)
        })
    }, [pageInfo])
    return {
        list: useAddKey(list),
        total,
        changePage
    }
}

export {
    useAuth,
    useAddKey,
    usePageInfo
}